      $ SET SOURCEFORMAT "FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID.  Transactions.
AUTHOR.  Raj Akula.
* Demonstrates how to read an Indexed file sequentially
* on any of its keys

* Enviorment division, don't really use this section
ENVIRONMENT DIVISION.

*Here i open a file the transaction file in sequential order
* i am just sorting it so its easier for me to print customer
* data towards the end. 
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT TransactionFile ASSIGN TO "sortedtransactions.dat"
          ORGANIZATION IS LINE SEQUENTIAL
          ACCESS MODE IS SEQUENTIAL.
		  
   SELECT PresortedTransactionFile ASSIGN TO "transactions.dat"
          ORGANIZATION IS LINE SEQUENTIAL
          ACCESS MODE IS SEQUENTIAL.
		  
   SELECT WorkFile ASSIGN TO "Work.tmp".

      
* Here i am defining the records in the files how they are to be read
*  as well as how they are to be written. 
DATA DIVISION.
FILE SECTION.
FD PreSortedTransactionFile.
01 PresortedTransactionRecord.
   88 PreEndOfFile VALUE HIGH-VALUE.
   02 PreTransCustNo       PIC 9(5).
   02 Filler               PIC X(5).
   02 PreTransProductNo    PIC X(6).
   02 Filler               PIC X(6).
   02 PreQuantity          PIC 9.
   02 Filler               PIC X(5).
   02 PreSaleCode          PIC X(1).
 
FD TransactionFile.
01 TransactionRecord.
   88 EndOfFile VALUE HIGH-VALUE.
   02 CustNo               PIC 9(5).
   02 Filler               PIC X(5).
   02 ProductNo            PIC X(6).
   02 Filler               PIC X(6).
   02 Quantity             PIC 9.
   02 Filler               PIC X(5).
   02 SaleCode             PIC X(1).
   
SD WorkFile.
01 WorkTransactionRecord.
*   88 PreEndOfFile VALUE HIGH-VALUE.
   02 WorkCustNo       PIC 9(5).
   02 Filler               PIC X(5).
   02 WorkProductNo    PIC X(6).
   02 Filler               PIC X(6).
   02 WorkQuantity          PIC 9.
   02 Filler               PIC X(5).
   02 WorkSaleCode          PIC X(1).   
 

* Use this to make sure read happened correctly
WORKING-STORAGE SECTION.
01   TransactionStatus              PIC X(2).
        
  
*Here i am just defining a sort method to iterate through the 
*  records.  
PROCEDURE DIVISION.
Begin.
   SORT  WorkFile ON ASCENDING KEY WorkCustNo
   INPUT PROCEDURE is GetAllRecords
   GIVING TransactionFile.
   STOP RUN.
   
GetAllRecords.
   OPEN INPUT PreSortedTransactionFile
   READ PreSortedTransactionFile
      AT END SET PreEndOfFile TO TRUE
   END-READ
   PERFORM UNTIL PreEndOfFile
      MOVE PresortedTransactionRecord TO WorkTransactionRecord
	  RELEASE WorkTransactionRecord
      READ PreSortedTransactionFile
        AT END SET PreEndOfFile TO TRUE
      END-READ 
   END-PERFORM
   CLOSE PreSortedTransactionFile.
